﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Form;
using PMPUtils.Maintenance;

namespace PMP.Maintenance
{
    public partial class ChecklistMaintenance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);
        }

        protected void ddlComponent_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshForm();
        }

        private void RefreshForm()
        {
            String value = ddlComponent.SelectedValue.Trim();

            if (value.Equals("000-Create-New"))
            {
                ddlComponent.Visible = false;
                txtComponent.Visible = true;
            }
            else
            {
                ddlComponent.Visible = true;
                txtComponent.Visible = false;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!CheckRequiredFields())
            {
                Messenger.Show(Response, "Please fill all required fields then proceed!");
                return;
            }

            // Derive all the values
            String component = ddlComponent.SelectedValue.Trim();
            if (txtComponent.Visible == true) { component = txtComponent.Text.Trim(); }

            String item = txtChecklistItem.Text.Trim();
            String itemDesc = txtItemDesc.Text.Trim();

            // Valdiate exists
            try
            {
                if (ChecklistMaintain.IsChecklistAlreadyExists(component, item))
                {
                    Messenger.Show(Response, "Checklist Item Already Exists!");
                    return;
                }
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            // Add Component
            try
            {
                ChecklistMaintain.AddChecklist(component, item, itemDesc, null, null);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            grvCheckLists.DataBind();
            CleanUp();

            Messenger.Show(Response, "Checklist Item Added Successfully!");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            CleanUp();
        }

        private bool CheckRequiredFields()
        {
            String component = ddlComponent.SelectedValue.Trim();
            String item = txtChecklistItem.Text.Trim();
            String itemDesc = txtItemDesc.Text.Trim();

            if (component.Equals(String.Empty) ||
                item.Equals(String.Empty) ||
                itemDesc.Equals(String.Empty))
            {
                return false;
            }

            return true;
        }

        private void CleanUp()
        {
            ddlComponent.Visible = true;
            ddlComponent.SelectedIndex = 0;

            txtComponent.Visible = false;
            txtComponent.Text = String.Empty;

            txtChecklistItem.Text = String.Empty;
            txtItemDesc.Text = String.Empty;
        }
    }
}
